package com.example.service;
import com.example.entity.SensitiveWord;
import com.example.mapper.SensitiveWordMapper;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SensitiveWordService {

    @Resource
    private SensitiveWordMapper sensitiveWordMapper;

    private List<String> sensitiveWords;

    @PostConstruct
    public void init() {
        refreshSensitiveWords();
    }

    public void refreshSensitiveWords() {
        List<SensitiveWord> list = sensitiveWordMapper.selectAll();
        sensitiveWords = list.stream().map(SensitiveWord::getWord).collect(Collectors.toList());
    }

    public List<String> getSensitiveWords() {
        return sensitiveWords;
    }

    // 管理功能
    public void addSensitiveWord(SensitiveWord sensitiveWord) {
        sensitiveWordMapper.insert(sensitiveWord);
        refreshSensitiveWords();
    }

    public void deleteSensitiveWord(Integer id) {
        sensitiveWordMapper.deleteById(id);
        refreshSensitiveWords();
    }

    public void updateSensitiveWord(SensitiveWord sensitiveWord) {
        sensitiveWordMapper.updateById(sensitiveWord);
        refreshSensitiveWords();
    }

    public List<SensitiveWord> getAllSensitiveWords() {
        return sensitiveWordMapper.selectAll();
    }
}